// json转树
function transformTozTreeFormat(sNodes) {
	sNodes = $.parseJSON(sNodes);
	var i, l, key = "id", parentKey = "parentId", childKey = "child";
	if (!key || key == "" || !sNodes)
		return [];
	var r = [];
	var tmpMap = [];
	for (i = 0, l = sNodes.length; i < l; i++) {
		tmpMap[sNodes[i][key]] = sNodes[i];
	}
	for (i = 0, l = sNodes.length; i < l; i++) {
		if (tmpMap[sNodes[i][parentKey]]
				&& sNodes[i][key] != sNodes[i][parentKey]) {
			if (!tmpMap[sNodes[i][parentKey]][childKey])
				tmpMap[sNodes[i][parentKey]][childKey] = [];
			tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]);
		} else {
			r.push(sNodes[i]);
		}
	}
	return r;
}
$(function(){
	//获取组织编号
	$.ajax({
		type:'post',
		url:server_url+'/rest/organization/getno.do?ran='+Math.random()+'&token='+token,
		success:function(data){
			if(data.retCode==="0000"){
				$("#org_No").html(data.data);
				$("#orgNo").val(data.data);
			}else{
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:'系统异常'
					},
					autoClose : 2000
				});
				console.error(data.retMsg);
			}
		}
	});
	//获取上级组织
	$.ajax({
		type:'post',
		data:{maxlevel:'4'},
		url:server_url+'/rest/organization/getparent.do?ran='+Math.random()+'&token='+token,
		dataType:'json',
		success:function(data){
			$("#parentId").html("<option value=\"0\" level=\"0\" orgPath=\"\">顶级组织</option>");
			var preLevel = '&nbsp;&nbsp;';
			if(data.retCode==="0000"){
				var datastr = transformTozTreeFormat(data.data);
				for(var i in datastr){
					preLevel = "&nbsp;&nbsp;";
					var tmp1 = datastr[i];
					$("#parentId").append("<option value=\""+tmp1.id+"\" level=\""+tmp1.level+"\" orgPath=\""+tmp1.orgPath+"\">"+preLevel+tmp1.orgName+"</option>");
					for(var j in tmp1.child){
						toAppendOrgName(tmp1.child[j]);
					}
				}
			}else{
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:'系统异常'
					},
					autoClose : 2000
				});
				console.error(data.retMsg);
			}
		}
	});
	
	$("#oform").validate({
		messages:{
			orgName:"该值不能为空!"
		},
		submitHandler:function(){
			sub();
		},
		errorPlacement: function(error, element) {
		    /*element.next().next().html(error);*/
			element.parent().append(error);
		}
		
	});
});
/**
 * 递归拼接树状3-4层
 */
function toAppendOrgName(tmp2){
	/*var tmp2 = tmp1.child[j];*/
	preLevel = "&nbsp;&nbsp;&nbsp;&nbsp;";
	$("#parentId").append("<option value=\""+tmp2.id+"\" level=\""+tmp2.level+"\" orgPath=\""+tmp2.orgPath+"\">"+preLevel+tmp2.orgName+"</option>");
	for(var k in tmp2.child){
		preLevel = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		var tmp3 = tmp2.child[k];
		$("#parentId").append("<option value=\""+tmp3.id+"\" level=\""+tmp3.level+"\" orgPath=\""+tmp3.orgPath+"\">"+preLevel+tmp3.orgName+"</option>");
		for(var l in tmp3.child){
			preLevel = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			var tmp4 = tmp3.child[l];
			$("#parentId").append("<option value=\""+tmp4.id+"\" level=\""+tmp4.level+"\" orgPath=\""+tmp4.orgPath+"\">"+preLevel+tmp4.orgName+"</option>");
		}
	}
}
function toBack(){
	url = 'orgList.do';
	tl_config.loadToWorkArea(url);
}
function sub(){
	var orgName = $("#orgName").val();
	if(orgName!=''){
		var addlevel = $("#parentId").find("option:selected").attr("level")-0+1;
		var orgPath = $("#parentId").find("option:selected").attr("orgPath");
		var username=localStorage.getItem("username");
		$("#oform").append("<input type=\"hidden\" name=\"level\" value=\""+addlevel+"\"/>");
		$("#oform").append("<input type=\"hidden\" name=\"addUser\" value=\""+username+"\"/>");
		$("#oform").append("<input type=\"hidden\" name=\"modifyUser\" value=\""+username+"\"/>");
		$("#oform").append("<input type=\"hidden\" name=\"orgPath\" value=\""+orgPath+"\"/>");
		console.debug(orgPath);
		$.ajax({
			type:'post',
			data:$("#oform").serialize(),
			url:server_url+'/rest/organization/addorg.do?ran='+Math.random()+'&token='+token,
			success:function(data){
				if(data.retCode==="0000"){
					toBack();
				}else{
					easyDialog.open({
						container:{
							header:'温馨提示',
							content:'系统异常'
						},
						autoClose : 2000
					});
					console.error(data.retMsg);
				}
			}
		});
	}else{
		easyDialog.open({
			container:{
				header:'温馨提示',
				content:'请输入组织名称！'
			},
			autoClose : 2000
		});
	}
}